<template>
  <div id="login-top">
    <div id="ms-login">
      <!--标题-->
      <div id="ms-title">后台管理系统</div>
      <!--用户名-->
      <el-form label-width="0px" id="ms-content">
        <el-form-item prop="userName">
          <el-input v-model="userName" placeholder="userName">
            <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
          </el-input>
        </el-form-item>
        <!--密码-->
        <el-form-item prop="password">
          <el-input type="password" placeholder="password" v-model="passWord">
            <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
          </el-input>
        </el-form-item>
        <!--登录-->
        <div id="login-btn">
          <el-button type="primary" @click="login()">登录</el-button>
        </div>
        <!--备注-->
        <p id="login-tips">如果登录失败，不要哭，专业写前端BUG团队！</p>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userName: "root",
      passWord: "123456",//不对就是tiger
    }; //定义数据
  },
  created() {
    localStorage.clear();
    //created在模板渲染成html前调用，通常在这里初始化某些属性，然后再渲染视图
  },
  methods: {
    //方法事件处理
    login() {
      var _this = this;
      console.log("登录。。。。。。。。。。。。");
      if (this.userName == "" || this.passWord == "") {
        _this.$message.error("用户名和密码不能为空！");
        return;
      }
      localStorage.setItem("userName", this.userName);
      //用户登录
      this.$http
        .get(
          "/userAuth/authentication?userName=" +
            this.userName +
            "&password=" +
            this.passWord
        )
        .then(function (response) {
          if (response.data.code == 200) {
            // alert("登录成功！token:"+response.data.data);
            //向本地存储对象存入token
            localStorage.setItem("token", response.data.data);
            //获取当前用户拥有权限按钮列表，放入localStorage
            _this.$http
              .get("/menu/queryByUserNameBtn")
              .then(function (response) {
                if (response.data.code == 200) {
                  localStorage.setItem(
                    "userButtonPermissions",
                    response.data.data
                  );
                  console.log(JSON.stringify(localStorage.getItem("userButtonPermissions")));
                }
              });
            //跳转页面
            _this.$router.push("/Index");
          } else {
            _this.$message.error("用户名或者密码错误");
          }
        })
        .catch(function (error) {
          console.log(error);
          _this.$message.error("你错了。。。");
        });
    },
    reset() {
      this.userName = "";
      this.passWord = "";
    },
  },
  components: {
    //组件注册
  },
};
</script>
<style>
#login-top {
  position: relative;
  width: 100%;
  height: 700px;
  /* background-color: aqua; */
  background-image: url(../assets/imgs/loginBg.jpg);
  background-size: 100%;
}
#ms-title {
  width: 100%;
  line-height: 50px;
  text-align: center;
  font-size: 20px;
  color: rgb(7, 7, 7);
  border-bottom: 1px solid #ddd;
}
#ms-login {
  position: absolute;
  left: 50%;
  top: 350px;
  width: 350px;
  margin: -190px 0 0 -175px;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.3);
  overflow: hidden;
}
#ms-content {
  padding: 30px 30px;
}
#login-btn {
  text-align: center;
}
#login-btn button {
  width: 100%;
  height: 36px;
  margin-bottom: 10px;
}
#login-tips {
  font-size: 12px;
  line-height: 30px;
  color: rgb(8, 8, 8);
}
</style>
